import { Component, ComponentType } from "../index";
import { getId } from "../../core/utils";
import { Pass } from "./Pass";
import { Engine } from "../../Engine";
/**
 * @memberof w.component
 */
class PostProcessing extends Component {
  constructor(opts = {}) {
    opts.type = ComponentType.PostProcessing;
    super(opts);
    this._effects = [];
  }
  get effects() {
    return this._effects;
  }
  get actives() {
    let _actives = [];
    for (let i = 0; i < this.effectCount; i++) {
      if (this.effects[i].enable) {
        _actives.push(this.effects[i]);
      }
    }
    return _actives;
  }
  get effectCount() {
    return this.effects.length;
  }
  get(object) {
    let id = getId(object);
    let out = null;
    for (let i = 0; i < this.effectCount; i++) {
      if (this.effects[i].id === id) {
        out = this.effects[i];
        break;
      }
    }
    return out;
  }
  getByType() {
    let out = null;
    for (let i = 0; i < this.effectCount; i++) {
      if (this.effects[i].type === type) {
        out = this.effects[i];
        break;
      }
    }
    return out;
  }
  rebuild() {
    let last = -1;
    for (let i = 0; i < this.effectCount; i++) {
      this.effects[i].renderToScreen = false;
      if (this.effects[i].enable) {
        last = i;
      }
    }
    if (last >= 0) {
      this.effects[last].renderToScreen = true;
    }
    this.update();
  }
  add(effect) {
    if (this.has(effect) == -1) {
      effect.bind(this);
      this.effects.push(effect);
    }
    this.rebuild();
    return this;
  }
  remove(effect) {
    const id = getId(effect);
    for (let i = this.effectCount - 1; i >= 0; i--) {
      if (this.effects[i].id === id) {
        this.effects.splice(i, 1);
        effect.unBind(this);
      }
    }
    this.rebuild();
    return this;
  }
  removeByType(type) {
    for (let i = this.effectCount - 1; i >= 0; i--) {
      if (this.effects[i].type === type) {
        this.splice(i, 1);
      }
    }
    return this;
  }
  raise(effect) { }

  has(effect) {
    let index = -1;
    for (let i = 0; i < this.effectCount; i++) {
      if (this.effects[i].type == effect.type) {
        index = i;
      }
    }
    return index;
  }
  update() {
    this.event.mode = "value";
    this.publish(this.event);
  }
}
export { PostProcessing };
